discard

set more off 


/** extract derived data sets **/
set more off 
use "$healthsave\prevalence and onsets_w7.dta", clear 

	/** cleaning info **/
		preserve 
		keep idauniq howcl??_w? howclnew??_w? 	
		drop *w0 
		save "$healthsave\cleaning info wide_with_W7.dta", replace 
		ren *_w? *? 
		local foreshape 
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
			local foreshape `foreshape' howcl`x' 
		}		
		reshape long `foreshape' howclnewst howclnewmi howclnewca , i(idauniq) j(wave) 
		save "$healthsave\cleaning info long_with_W7.dta", replace 
		restore 
		
	/** interview information **/
		preserve
		keep idauniq inwave* alive_w? agew? intyear* intmonth* firstwave lastwave 
		drop *w0 
		ren agew? age_w? 
		save "$healthsave\interview info wide_with_W7.dta", replace 
		ren inwave inwaveall 
		ren alive_w? alive? 
		ren age_w? age? 
		ren intyear_w? intyear? 
		ren intmonth_w? intmonth? 
		reshape long inwave alive age intyear intmonth  ///
				, i(idauniq) j(wave) 
		save "$healthsave\interview info long_with_W7.dta", replace 
		restore 
		
	/** basic health variables only **/ 
		
		preserve 
		keep idauniq ever??_w? onset??_w? new??_w?  
		drop *w0 		
		
		save "$healthsave\derived health vars_with_W7.dta", replace 
		restore 
		
		preserve 
		keep idauniq ever??_w? onset??_w? new??_w? inwave? 
		drop *w0 		
			
		forv i = 1/7 { 
	
	/*******************/	
	/** MAJOR ILLNESS **/
	/*******************/
	
		/** ever diagnosed **/
		
		egen evermaj_w`i' = anymatch(everan_w`i' evermi_w`i' everhf_w`i' ///
									  everst_w`i' everlu_w`i' everca_w`i') ///
									  , v(1) 
			
		replace evermaj_w`i' = . if inwave`i' == 0
		lab var evermaj_w`i' "Ever diagnosed with major illness (an/mi/hf/st/lu/ca)" 
		
		egen msng_evermaj_w`i' = anymatch(everan_w`i' evermi_w`i' everhf_w`i' ///
										  everst_w`i' everlu_w`i' everca_w`i') ///
										  , v(-1) 
		replace msng_evermaj_w`i' = 0 if evermaj_w`i' == 1 
		lab var msng_evermaj_w`i' "Missing info on major illness diagnoses" 
		replace msng_evermaj_w`i' = . if inwave`i' == 0		
		
		
		/** new diagnoses **/
		
		if `i' > 1 { 
			local Pi = `i'-1 
			egen newmaj_w`i' = anymatch(newan_w`i' onsetmi_w`i' newhf_w`i' ///
									  onsetst_w`i' newlu_w`i' onsetca_w`i') ///
									  , v(1) 
			replace newmaj_w`i' = . if inwave`i' == 0 | inwave`Pi' == 0 
			lab var newmaj_w`i' "New diagnosis of major illness" 
			
			egen msng_newmaj_w`i' = anymatch(newan_w`i' onsetmi_w`i' newhf_w`i' ///
									  onsetst_w`i' newlu_w`i' onsetca_w`i') ///
									  , v(-1) 
			replace msng_newmaj_w`i' = 0 if newmaj_w`i' == 1 						  
			replace msng_newmaj_w`i' = . if inwave`i' == 0 | inwave`Pi' == 0 
			}
		
		
		
	/*******************/	
	/** MINOR ILLNESS **/
	/*******************/
		
		/** ever diagnosed **/ 
		
		egen evermin_w`i' = anymatch(everbp_w`i' everdi_w`i' everar_w`i'), ///
											v(1) 
		replace evermin_w`i' = . if inwave`i' == 0 
		lab var evermin_w`i' "Ever diagnosed with minor illness (bp di ar)"
		egen msng_evermin_w`i' = anymatch(everbp_w`i' everdi_w`i' everar_w`i'), ///
											v(-1) 
		replace msng_evermin_w`i' = 0 if evermin_w`i' == 1
		replace msng_evermin_w`i' = . if inwave`i' == 0 
		lab var msng_evermin_w`i' "Missing info on minor illness diagnoses" 
		
		
		/** new diagnoses **/ 
		
		if `i' > 1 { 
			egen newmin_w`i' = anymatch(newbp_w`i' newdi_w`i' newar_w`i') ///
									  , v(1) 
			replace newmin_w`i' = . if inwave`i' == 0 | inwave`Pi' == 0 
			lab var newmin_w`i' "New diagnosis of major illness" 
			
			egen msng_newmin_w`i' = anymatch(newbp_w`i' newdi_w`i' newar_w`i') ///
									  , v(-1) 
			replace msng_newmin_w`i' = 0 if newmin_w`i' == 1 
			replace msng_newmin_w`i' = . if inwave`i' == 0 | inwave`Pi' == 0 
		}
	}
		
		save "$healthsave\derived health variables wide_with_W7.dta", replace 

		ren *_w? *? 
		local foreshape 
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch min maj { 
			local foreshape `foreshape' ever`x' new`x' 
		}
		reshape long `foreshape' onsetca onsetmi onsetst msng_evermaj msng_evermin msng_newmaj msng_newmin , i(idauniq) j(wave)
		save "$healthsave\derived health variables long_with_W7.dta", replace 
		restore 

		preserve 
		keep idauniq henmmiw2 henmstw2 ///
				agedmi_w1 agedmi_w2 ///
				agedst_w1 agedst_w2 /// 
				yrdmi_w2 yrdst_w2  
		
		save "$healthsave\incidence timing info_with_W7.dta", replace 
		restore 
		
		preserve 
		keep idauniq  agedbl_w? yrdbl_w? 
		save "$healthsave\blood disorder timing info_with_W7.dta", replace 
		restore 
		
		
		
		
		
